/**
 * Project Name:springmvc
 * File Name:TClubMemberController.java
 * Package Name:com.yichaoart.club.controller
 * Date:2016年5月12日下午3:12:52
 * Copyright (c) 2016,  All Rights Reserved.
 *
*/

package com.yichaoart.club.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.yichaoart.club.biz.TClubMemberServiceI;
import com.yichaoart.club.biz.TClubOrderServiceI;
import com.yichaoart.club.model.TClubOrder;

import common.CommonStant;

/**
 * ClassName:TClubMemberController <br/>
 * Date: 2016年5月12日 下午3:12:52 <br/>
 * 
 * @author i1093108915@yeah.net
 * @version
 * @since JDK 1.6
 * @see
 */
@Controller
@RequestMapping("/order")
public class TClubOrderController {

	@Autowired
	TClubOrderServiceI orderServiceI;
	
	@Autowired
	TClubMemberServiceI memberServiceI;

	/**
	 * addOrder:(这里用一句话描述这个方法的作用). <br/>
	 * @author i1093108915@yeah.net
	 * @param session
	 * @param request
	 * @param response
	 * @param order
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "addOrder")
	public ModelAndView addOrder(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			TClubOrder order) {
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		order.setSid(UUID.randomUUID().toString().replace("-", ""));
		order.setCreateTime(new Date());
		order.setPstatus("shipping");
//		TClubMember member = memberServiceI.selectByPrimaryKey(sid)
		int result = orderServiceI.insertOrder(order);
		if (result > 0) {
			mv.addObject("oprReulst", CommonStant.OPR_RESUT_SUC);
		} else {
			mv.addObject("oprReulst", CommonStant.OPR_RESULT_FAIL);
		}

		mv.setViewName("oprResult");
		return mv;
	}
	
	/**
	 * getMember:(这里用一句话描述这个方法的作用). <br/>
	 *
	 * @author i1093108915@yeah.net
	 * @param session
	 * @param request
	 * @param response
	 * @param sid
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "getOrder")
	public ModelAndView getOrder(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			String sid) {
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		TClubOrder order =orderServiceI.selectByPrimaryKey(sid);
		mv.addObject("order", order);
		mv.setViewName("viewOrder");
		return mv;
	}
	
	@RequestMapping(value = "updateOrder")
	public ModelAndView updateOrder(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			TClubOrder record) {
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		int result =orderServiceI.updateByPrimaryKeySelective(record);
		if (result > 0) {
			mv.addObject("oprReulst", CommonStant.OPR_RESUT_SUC);
		} else {
			mv.addObject("oprReulst", CommonStant.OPR_RESULT_FAIL);
		}
		mv.setViewName("oprResult");
		return mv;
	}
	
	@RequestMapping(value = "queryOrderList")
	public ModelAndView queryOrderList(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false, defaultValue = "-1") int start,
			@RequestParam(required = false, defaultValue = "-1")int limit) {
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		request.setAttribute("nav_active", "queryOrderList");
		List<TClubOrder> orders =orderServiceI.queryOrderList(start,limit);
		mv.addObject("order", orders);
		mv.setViewName("orderList");
		return mv;
	}
	
	/**
	 * checkOnline:(校验是否在线). <br/>
	 *
	 * @author i1093108915@yeah.net
	 * @param request
	 * @return
	 * @since JDK 1.6
	 */
	private boolean checkOnline(HttpServletRequest request) {
		if (request.getSession().getAttribute(CommonStant.LOGIN_ONLINE)==null) {
			return false;
		}
		return true;
	}
}
